package gui.frames;

import gui.GuiManager;
import gui.GuiManager.FramesEnum;
import gui.actions.StatisticsAction;
import gui.components.FancyButton;
import gui.panels.CopyrightPanel;
import gui.panels.CopyrightPanel.ButtonTypeEnum;
import gui.utils.ComponentUtils;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MainMenuFrame extends JFrame {
	
	private static final long serialVersionUID = 1L;

	public MainMenuFrame() {
		buildFrame();

		pack();
        setResizable(false);
        ComponentUtils.centerOnScreen(this);

        ComponentUtils.installWindowClosingListener(this);
	}

	@Override
    public String getTitle() {
        return "Welcome to Fictionator";
    }

	private void buildFrame() {
        setTitle(getTitle());
        setContentPane(contentBuild());
    }

	private Container contentBuild() {

        JLabel logo = new JLabel(ComponentUtils.readImageIcon("logo1.png"));
        Box containerBox = Box.createVerticalBox();
        containerBox.add(Box.createVerticalStrut(12));

        FancyButton fancyButtonPanel;
		fancyButtonPanel = new FancyButton(ComponentUtils.readImageIcon("launch.png"), "Launch game");
        containerBox.add(fancyButtonPanel);
        containerBox.add(Box.createVerticalStrut(12));
        fancyButtonPanel.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent evt) {
        		SwingUtilities.invokeLater(new Runnable() {
        			public void run() {
        				GuiManager.switchFrame(FramesEnum.PLAY_FRAME);
        			}
        		});
        	}
        });

        fancyButtonPanel = new FancyButton(ComponentUtils.readImageIcon("statistics.png"), "Statistics");
        containerBox.add(fancyButtonPanel);
        containerBox.add(Box.createVerticalStrut(12));
        fancyButtonPanel.addActionListener(new StatisticsAction(containerBox)); 

        fancyButtonPanel = new FancyButton(ComponentUtils.readImageIcon("configuration.png"), "Configuration");
        containerBox.add(fancyButtonPanel);
        containerBox.add(Box.createVerticalStrut(12));
        fancyButtonPanel.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent evt) {
        		GuiManager.switchFrame(FramesEnum.CONFIGURATION_FRAME);
        	}
        });
        
        final JFrame frame = this;
        CopyrightPanel copyrightPanel = new CopyrightPanel(ButtonTypeEnum.QUIT_BUTTON, new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ComponentUtils.showQuitDialog(frame);
			}
		});

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(BorderLayout.NORTH, logo);
        panel.add(BorderLayout.CENTER, containerBox);
        panel.add(BorderLayout.SOUTH, copyrightPanel);

		return panel;
	}
	
}
